package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.concurrent.ThreadPoolExecutor;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    @Value("${reggie.salt}")
    private String salt;

    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request,@RequestBody Employee employee){
        String password = employee.getPassword();
        password += salt;
        password=DigestUtils.md5DigestAsHex(password.getBytes());


        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<Employee>();
        lqw.eq(Employee::getUsername,employee.getUsername());

        Employee emp = employeeService.getOne(lqw);
        if (emp == null){
            return R.error("用户名错误");
        }
        if (!emp.getPassword().equals(password)){
            return R.error("密码错误");
        }

        if (emp.getStatus() == 0){
            return R.error("该用户账号已被禁用");
        }


        request.getSession().setAttribute("employee",emp.getId());
        return R.success(emp);


    }

    @PostMapping("/logout")
    public R<String> loginOut(HttpServletRequest request, HttpSession httpSession){
        request.getSession().removeAttribute("employee");
        //也可以直接注入session对象
        //方法一: 删除session中的一个键值对: httpSession.removeAttribute("employee");
        //方法二: 删除session中的一个对象: httpSession.invalidate();
        return R.success("退出成功");
    }


    @PostMapping
    public R<String> save(HttpServletRequest request,@RequestBody Employee employee){
        String password = DigestUtils.md5DigestAsHex(("123456" + salt).getBytes());
        employee.setPassword(password);
        employeeService.save(employee);
        return R.success("添加成功");

       /* log.info("新增员工，员工信息：{}",employee.toString());
        employee.setPassword(DigestUtils.md5DigestAsHex(("123456"+salt).getBytes()));
        employeeService.save(employee);
      return R.success("添加成功");*/
  }
  @GetMapping("/page")
   public R<Page> page(int page,int pageSize,String name){
       log.info("page = {},pageSize = {},name = {}" ,page,pageSize,name);;
       Page<Employee> pageInfo = new Page(page,pageSize);

       LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
       lqw.like(StringUtils.isNotEmpty(name),Employee::getName,name);
       lqw.orderByDesc(Employee::getCreateTime);
       employeeService.page(pageInfo,lqw);
       return R.success(pageInfo);
   }


    @PutMapping
    public R<String> update(HttpServletRequest request,@RequestBody Employee employee){
        employeeService.updateById(employee);
        return R.success("员工信息修改成功");
    }

    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id ){
        Employee employee = employeeService.getById(id);
        if (employee!=null){
            return R.success(employee);
        }
        return R.error("没有查询到员工信息");
    }
}
